Systemd

  • Purpose of an init system is:
    • Initialize the components that must be started after the Linux kernel is booted.
    • Manage services and daemons at any point while the system is running.
  • systemd features
    • Backwards compatible with SysV
    • Parallel startup of system services at boot time,
    • On-demand activation of daemons
    • Dependency-based service control logic
  • Units
    • Units are resources that systemd knows how to manage.
    • They are categorized by the type of resource they represent.
    - ex daemons are known as service units. *.service

Available Units

In [12]:
# Available units
systemctl -t help # --type help
Available unit types:
service
socket
busname
target
device
mount
automount
swap
timer
path
slice
scope

Different type of units:

Target unit .target A group of systemd units. Service unit .service A system service. Automount unit .automount A file system automount point. Device unit .device A device file recognized by the kernel Mount unit .mount A file system mount point. Path unit .path A file or directory in a file system. Scope unit .scope An externally created process. Slice unit .slice A group of hierarchically organized units that manage system processes. Snapshot unit .snapshot A saved state of the systemd manager. Socket unit .socket An inter-process communication socket. Swap unit .swap A swap device or a swap file. Timer unit .timer A systemd timer.

Targets

Used to group other units together and to bring the system to certain states. (Runlevel)

Default target:

In [13]:
systemctl get-default
graphical.target
In [ ]:
set-default graphical.target

Default target paths:

In [5]:
ll /etc/systemd/system/default.target
ll /lib/systemd/system/default.target
lrwxrwxrwx 1 root root 36 Dec  9 03:30 /etc/systemd/system/default.target -> /lib/systemd/system/graphical.target
lrwxrwxrwx 1 root root 16 Nov 25 16:27 /lib/systemd/system/default.target -> graphical.target

Going into a target:

In [ ]:
# AllowIsolate=yes (isolation must be yes)
systemctl isolate graphical.target # same as: telinit runlevel
[ multiuser, graphical, recue, emergency, reboot, poweroff ]
  • rescue = 1 user

    "rescue" is equivalent to the old runlevel 1 everything is mounted, but no normal services are started

  • emergency = nothing but systemd

    No services are started, no mount points mounted no sockets established, good for debugging step by step

In [ ]:
systemctl rescue # like isolate rescue but sends a wall message
systemctl --no-wall rescue
0 runlevel0.target, poweroff.target Shut down and power off the system. 1 runlevel1.target, rescue.target Set up a rescue shell. 2 runlevel2.target, multi-user.target Set up a non-graphical multi-user system. 3 runlevel3.target, multi-user.target Set up a non-graphical multi-user system. 4 runlevel4.target, multi-user.target Set up a non-graphical multi-user system. 5 runlevel5.target, graphical.target Set up a graphical multi-user system. 6 runlevel6.target, reboot.target Shut down and reboot the system.

Go into recovery (rescue) mode by editing grub:

In [ ]:
systemd.unit=rescue.target # same as old school: 1 at the end of kernel line

Managing services

In [ ]:
systemctl start name.service
systemctl stop name.service

# Stop, then start again.
systemctl restart name.service

# Restarts a service only if it is running.
systemctl try-restart name.service
Reload

If the application in question is able to reload its configuration files (without restarting), you can issue the reload command to initiate that process.

In [ ]:
# Reloads configuration.
systemctl reload name.service

# try to reload if can't restarts
systemctl reload-or-restart name.service
systemctl try-reload-or-restart name.service

Check service status

In [ ]:
systemctl status name.service

systemctl is-active name.service # running
systemctl is-enabled name.service # will be activated when booting
systemctl is-failed name.service # tried to active but failed

Change service status

In [ ]:
systemctl enable name.service
systemctl disable name.service

# combination of disable and enable and is useful to reset the symlinks
systemctl reenable name.service

Mask and unmasking

you can mask any service unit to prevent it from being started manually or by another service:

In [ ]:
# links /etc/systemd/system/name.service -> /dev/null
systemctl mask name.service
systemctl unmask name.service

List units

In [ ]:
# list (all kind of units) [loaded and active]
systemctl list-units

# --all = active or not (inactive, dead, not-found, etc)

# list (all kind of units) [loaded] active or not
systemctl list-units --all

# list (service units) [loaded and currently active] 
systemctl list-units --type=service

# list (target units) [loaded] active or not 
systemctl list-units --type=target --all

# list (service units) [loaded but currently inactive]
# if we don't use --all it's only shows active ones
systemctl list-units --all --type=service --state=inactive

# list (target units) [ltried to load but not found] 
systemctl list-units --all --type=target --state=not-found

list-units only displays units that systemd has attempted to load (wanted by).
To see every available unit file including those that systemd has not attempted to load:

In [ ]:
# list all [available] service units to see if they are enabled or not
systemctl list-unit-files --type service # can be used for other units like: targets etc.
In [ ]:
systemd-networkd-resolvconf-update.path    static 
accounts-daemon.service                    enabled
alsa-utils.service                         masked # /dev/null
console-getty.service                      disabled
In [17]:
systemctl list-unit-files --state masked | head -3
UNIT FILE                    STATE 
alsa-utils.service           masked
bootlogd.service             masked

static: unit file does not contain an "install" section. these units cannot be enabled and these are used only as a dependency of another units.

In [3]:
vimcat /lib/systemd/system/systemd-networkd-resolvconf-update.path
# vimcat /lib/systemd/system/colord.service
[Unit]
Description=Trigger resolvconf update for networkd DNS
ConditionPathIsSymbolicLink=/etc/resolv.conf

[Path]
PathChanged=/run/systemd/netif/state
In [5]:
vimcat /lib/systemd/system/accounts-daemon.service # systemctl cat accounts-daemon.service
[Unit]
Description=Accounts Service

# In order to avoid races with identity-providing services like SSSD or
# winbind, we need to ensure that Accounts Service starts after
# nss-user-lookup.target
After=nss-user-lookup.target
Wants=nss-user-lookup.target

[Service]
Type=dbus
BusName=org.freedesktop.Accounts
ExecStart=/usr/lib/accountsservice/accounts-daemon

[Install]
# We pull this in by graphical.target instead of waiting for the bus
# activation, to speed things up a little: gdm uses this anyway so it is nice
# if it is already around when gdm wants to use it and doesn't have to wait for
# it.
WantedBy=graphical.target

List dependecies

In [ ]:
systemctl list-dependencies multiuser.target
systemctl list-dependencies --after *.service/target
systemctl list-dependencies --before *.service/target

Other functionalities

In [ ]:
# Alias
systemctl show multi-user.target -p Names
 ⁠
# Controlling systemd on a Remote Machine
systemctl --host user_name@host_name command

Lecture notes

License

Creative Commons License

Linux Notes by Milad As (Ravexina) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


ravexina's gitlab

ravexina's github